package com._520it.crm.util;


import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class ExceptionHandler {
    @org.springframework.web.bind.annotation.ExceptionHandler(UnauthorizedException.class)
    public  void handleException(HandlerMethod handlerMethod, HttpServletResponse response){
        try {
            //页面请求没有权限就重定向到nopermission.jsp
            ResponseBody annotation = handlerMethod.getMethod().getAnnotation(ResponseBody.class);
            if(annotation == null){
                response.sendRedirect("/nopermission.jsp");
            }else{
                //Ajax请求 返回JSON格式数据 {}
                // 保存, 更新, 删除 {"success":false,"msg":"您没有权限"}
                //列表 {"total":0,"row":[]}  返回空列表
                response.setCharacterEncoding("utf-8");
                response.getWriter().println("{\"success\":false,\"msg\":\"您没有权限\",\"total\":0,\"row\":[]}");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
